@echo off
setlocal enabledelayedexpansion
:: 强制UTF-8编码
chcp 65001 > nul
set PYTHONIOENCODING=utf-8
REM 配置文件设置
set CONFIG_FILE=build_path.cfg
set PYTHON_RELATIVE_PATH=Python\python.exe

REM 读取配置文件
if not exist "%CONFIG_FILE%" (
    echo 错误: 配置文件 %CONFIG_FILE% 不存在
    exit /b 1
)

set BUILD_PATH=
for /f "usebackq delims=" %%i in ("%CONFIG_FILE%") do set "BUILD_PATH=%%i"

REM 验证路径有效性
if "%BUILD_PATH%"=="" (
    echo 错误: 配置文件内容为空
    exit /b 1
)

if not exist "%BUILD_PATH%\" (
    echo 错误: 构建路径不存在: %BUILD_PATH%
    exit /b 1
)

set PYTHON_EXE="%BUILD_PATH%\%PYTHON_RELATIVE_PATH%"
if not exist %PYTHON_EXE% (
    echo 错误: Python可执行文件不存在: %PYTHON_EXE%
    exit /b 1
)

@REM 获取当前路径
set CURRENT_PATH=%~dp0

REM 处理命令参数
if "%~1"=="" (
    echo 使用方法: %0 [命令]
    echo 可用命令:
    echo   package   打包D5Core
    echo   uninstall 卸载D5Core
    echo   install   安装D5Core（先卸载后安装）
    exit /b 0
)

REM 切换到构建目录
pushd "%BUILD_PATH%"

REM 根据参数执行不同操作
if /i "%~1"=="package" (
    echo 正在打包D5Core...
    %PYTHON_EXE% %CURRENT_PATH%\package.py

) else if /i "%~1"=="uninstall" (
    echo 正在卸载D5Core...
    %PYTHON_EXE% -m pip uninstall D5Core -y

) else if /i "%~1"=="install" (
    echo 正在卸载旧版本...
    %PYTHON_EXE% -m pip uninstall D5Core -y
    
    echo 正在安装新版本...
    %PYTHON_EXE% -m pip install --find-links=%CURRENT_PATH%\dist  D5Core

) else (
    echo 无效命令: %~1
    exit /b 1
)

popd
endlocal